## Copyright(c) 2017, Intel Corporation
##
## Redistribution  and  use  in source  and  binary  forms,  with  or  without
## modification, are permitted provided that the following conditions are met:
##
## * Redistributions of  source code  must retain the  above copyright notice,
##   this list of conditions and the following disclaimer.
## * Redistributions in binary form must reproduce the above copyright notice,
##   this list of conditions and the following disclaimer in the documentation
##   and/or other materials provided with the distribution.
## * Neither the name  of Intel Corporation  nor the names of its contributors
##   may be used to  endorse or promote  products derived  from this  software
##   without specific prior written permission.
##
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
## IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
## ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
## LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
## CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
## SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
## INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
## CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
## POSSIBILITY OF SUCH DAMAGE.

cmake_minimum_required(VERSION  3.10)
#include(packaging)


## All the scripts to install that aren't zipped, just copied.
set(PLATFORM_SCRIPTS
        afu_synth_setup
        rtl_src_config)

foreach(SRC ${PLATFORM_SCRIPTS})
  ## Substitute CMake variables of the form @<var>@ in the source file
  ## and store the file in the build tree's bin directory.
  configure_file(
          ${SRC} ${PROJECT_BINARY_DIR}/bin/${SRC}
          @ONLY NEWLINE_STYLE UNIX)
  ## This forces the configure_file to be built for all targets
  add_custom_target(${SRC} ALL DEPENDS ${PROJECT_BINARY_DIR}/bin/${SRC})

  install(PROGRAMS ${PROJECT_BINARY_DIR}/bin/${SRC}
          DESTINATION bin
          COMPONENT platform)
endforeach(SRC)


## Zip afu_platform_config and afu_platform_info with libraries.

# The set of files to include in the zip file.  Pass the name of AFU_PL_CFG
# above, which CREATE_PYTHON_EXE will detect as a path/name tuple.
set(PKG_PYTHON_FILES
    AFU_PL_CFG
    platmgr/__init__.py
    platmgr/jsondb.py
    platmgr/emitcfg.py)

foreach(SCRIPT afu_platform_config afu_platform_info)
  # Start with CMake variable substitution.
  configure_file(
          ${SCRIPT} ${CMAKE_CURRENT_BINARY_DIR}/bin/${SCRIPT}.py
          @ONLY NEWLINE_STYLE UNIX)

  # Following variable substitution, the updated file is in a build directory.
  # The zipped file must be created without the path to the build tree.  Generate
  # a tuple (list) with the file's path and the name to call it in the zip file.
  set(AFU_PL_CFG ${CMAKE_CURRENT_BINARY_DIR}/bin/${SCRIPT}.py
                 ${SCRIPT}.py)

  CREATE_PYTHON_EXE(${SCRIPT} ${SCRIPT} ${PKG_PYTHON_FILES})

  # The CREATE_PYTHON_EXE macro sets PACKAGER_BIN to the generated zip file.
  install(PROGRAMS ${PACKAGER_BIN}
          DESTINATION bin
          COMPONENT platform)
endforeach(SCRIPT)
